package com.yunji.web.product;

import com.yunji.aggregate.model.dto.product.PricingParam;
import com.yunji.aggregate.model.dto.product.ProductDTO;
import com.yunji.aggregate.model.vo.product.PricingProductStatisticsVO;
import com.yunji.aggregate.model.vo.product.ProductStatisticsVO;
import com.yunji.aggregate.service.ProductAggregateService;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.product.business.ProductBusiness;
import com.yunji.product.domain.ProductDO;
import com.yunji.product.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Package com.yunji.web.product
 * FileName:	ProductAgentController.java
 * Copyright:	Copyright © 2024 云集互联 All Rights Reserved
 * Company:		云集互联
 * @version: 1.0
 * @since: JDK 1.8.0_202
 * Create at:   2024-04-01 09:31:06
 * Description:
 * 商品信息交互
 * Modification History:
 * Author					Date						Version			Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-04-01 09:31:06			1.0				1.0 Version
 */
@Api(tags = "管理端-商品信息")
@RestController
@RequestMapping("/container/product")
public class ProductController extends BaseController {
    @Resource
    ProductBusiness business;

    @Resource
    ProductAggregateService aggregateService;

    @ApiOperation("查询商品信息列表")
    @PreAuthorize("@ss.hasPermi('container:product:query')")
    @GetMapping("/list")
    public Response<List<ProductStatisticsVO>> list(@Valid ProductDTO dto) {
        List<ProductStatisticsVO> list = aggregateService.searchProductList(dto);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("获取代理商下的商品信息")
    @GetMapping("/listByAgent")
    public Response<List<ProductStatisticsVO>> listByAgent(ProductDTO dto) {
        List<ProductStatisticsVO> list = aggregateService.agentSearch(dto);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出商品信息列表")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('container:product:export')")
    @PostMapping("/export")
    public void export(ProductDTO productDTO) {
        List<ProductStatisticsVO> list = aggregateService.searchProductList(productDTO);
        ExcelUtil<ProductStatisticsVO> util = new ExcelUtil<>(ProductStatisticsVO.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    @ApiOperation("获取商品信息详细信息")
    @PreAuthorize("@ss.hasPermi('container:product:info')")
    @GetMapping(value = "/{id}")
    public Response<ProductVO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(business.select(id));
    }

    @ApiOperation("新增商品信息")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('container:product:add')")
    @PostMapping
    public Response<Long> create(@RequestBody ProductVO product) {
        return ResponseHelper.success(business.create(product));
    }

    @ApiOperation("修改商品信息")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('container:product:edit')")
    @PutMapping
    public Response<String> update(@RequestBody ProductVO product) {
        business.update(product);
        return ResponseHelper.success();
    }

    @ApiOperation("删除商品信息")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('container:product:remove')")
    @DeleteMapping("/{ids}")
    public Response<String> remove(@PathVariable Long... ids) {
        business.delete(ids);
        return ResponseHelper.success();
    }

    @ApiOperation("代理商定价列表")
    @GetMapping("/agentPricingList")
    public Response<List<PricingProductStatisticsVO>> agentPricingList(PricingParam pricingParam) {
        pricingParam.setUserType(UserType.AGENT.getCode());
        List<PricingProductStatisticsVO> list = aggregateService.searchAgentPrice(pricingParam);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("网点/经理用户定价列表")
    @GetMapping("/managerPricingList")
    public Response<List<PricingProductStatisticsVO>> managerPricingList(PricingParam pricingParam) {
        pricingParam.setUserType(UserType.SITE.getCode());
        List<PricingProductStatisticsVO> list = aggregateService.searchAgentPrice(pricingParam);
        return ResponseHelper.successWithPage(list);
    }

    /*----------------------------------------搜索栏-------------------------------------*/

    @ApiOperation("商品名称匹配搜索")
    @GetMapping("/searchName")
    public Response<List<String>> searchName(@RequestParam("name") String name) {
        return ResponseHelper.successWithPage(business.searchName(name));
    }

    @ApiOperation("代理商定价名称匹配搜索")
    @GetMapping("/searchAgentName")
    public Response<List<String>> searchAgentName(@RequestParam("agentName") String agentName) {
        return ResponseHelper.successWithPage(aggregateService.searchAgentName(agentName));
    }

    @ApiOperation("经理用户定价名称匹配搜索")
    @GetMapping("/searchManagerName")
    public Response<List<String>> searchManagerName(@RequestParam("managerName") String managerName) {
        return ResponseHelper.successWithPage(aggregateService.searchManagerName(managerName));
    }
}
